package model

import (
	"gorm.io/gorm"
	"time"
	"video_08_28/global"
)

type Video struct {
	Id        uint32         `gorm:"column:id;type:int UNSIGNED;comment:视频ID;primaryKey;not null;" form:"id" json:"id"`           // 视频ID
	UserId    int32          `gorm:"column:user_id;type:int;comment:发布用户ID;not null;" form:"user_id" json:"user_id"`              // 发布用户ID
	Title     string         `gorm:"column:title;type:varchar(36);comment:标题;not null;" form:"title" json:"title"`                // 标题
	VideoUrl  string         `gorm:"column:video_url;type:varchar(500);comment:视频地址;not null;" form:"video_url" json:"video_url"` // 视频地址
	Info      string         `gorm:"column:info;type:varchar(300);comment:视频简介;not null;" form:"info" json:"info"`                // 视频简介
	LikeNum   int32          `gorm:"column:like_num;type:int;comment:喜欢数量;not null;default:0;" form:"like_num" json:"like_num"`   // 喜欢数量
	CreatedAt time.Time      `gorm:"column:created_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" form:"created_at" json:"created_at"`
	UpdatedAt time.Time      `gorm:"column:updated_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" form:"updated_at" json:"updated_at"`
	DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;type:datetime(3);default:NULL;" form:"deleted_at" json:"deleted_at"`
}

func (v *Video) TableName() string {
	return "video"
}

func (v *Video) AddVideo() error {
	return global.DB.Create(&v).Error
}

// offset := (page - 1) * pageSize
func (v *Video) FindVideo(find string, page int, size int) (videos []Video, err error) {
	err = global.DB.Where("title like ?", "%"+find+"%").Offset((page - 1) * size).Find(&videos).Error
	return
}

func (v *Video) GetVideo() error {
	return global.DB.Where("id = ?", v.Id).Find(&v).Error
}
